/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title
 * @Description Protected a pair inside a thread-safe class:
 * @Package concurrent.mthread.synchronizeCodeBlock
 * @Since 2021/8/18 2:29 下午
 */
package concurrent.mthread.synchronizeCodeBlock;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

abstract class PairManager {
    /**
     * @Description 检查计数
     * @Author chentl
     * @Create: 2021/8/18 3:03 下午
     */
    AtomicInteger checkCounter = new AtomicInteger(0);

    protected Pair p = new Pair();

    private List<Pair> storage = Collections.synchronizedList(new ArrayList<>());

    public synchronized Pair getPair() {
        //Make a copy to keep origin safe
        return new Pair(p.getX(), p.getY());
    }

    protected void store(Pair p){
        //存储 p
        storage.add(p);

        //线程等待 50 ms
        try {
            TimeUnit.MILLISECONDS.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    abstract void increment();

}
